package com.sz.szaiagent;

import org.springframework.ai.autoconfigure.vectorstore.pgvector.PgVectorStoreAutoConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

@SpringBootApplication(exclude = PgVectorStoreAutoConfiguration.class)
public class SzAiAgentApplication {

    public static void main(String[] args) {
        SpringApplication.run(SzAiAgentApplication.class, args);
    }
    @Bean
    public CorsFilter corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration config = new CorsConfiguration();

        // 允许跨域的源
        config.addAllowedOrigin("http://localhost:3000"); // 前端开发服务器地址
        config.addAllowedOrigin("http://localhost:5173"); // Vite 默认地址

        // 允许跨域的请求头
        config.addAllowedHeader("*");

        // 允许跨域的请求方法
        config.addAllowedMethod("*");

        // 允许携带认证信息（cookies等）
        config.setAllowCredentials(true);

        // 暴露响应头
        config.addExposedHeader("*");

        // 设置预检请求的有效期，单位为秒
        config.setMaxAge(3600L);

        source.registerCorsConfiguration("/**", config);
        return new CorsFilter(source);
    }
}
